package com.example.administrator.test;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Administrator on 2017/9/1.
 */

public class Tab3ListAdapter extends BaseAdapter {
    Context context;
    List<Book> list;
    LayoutInflater inflater;

    //设置接口
    private View.OnClickListener collect;
    //设置接口方法
    public void setOnButton(View.OnClickListener collect)
    {
        this.collect = collect;
    }

    public Tab3ListAdapter(Context context,List<Book> list){
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        //获取当前第几个资源
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if(view == null){
            view = inflater.inflate(R.layout.tab3_list_item,null);
            viewHolder = new ViewHolder();
            viewHolder.img = (ImageView)view.findViewById(R.id.tab3_list_item_image);
            viewHolder.textView = (TextView)view.findViewById(R.id.tab3_list_item_text);
            viewHolder.collect = (ImageView)view.findViewById(R.id.tab3_list_item_collect);
            viewHolder.collect.setOnClickListener(collect);
            viewHolder.collect.setTag(i);
            view.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder)view.getTag();
        }

        viewHolder.img.setImageResource(context.getResources().getIdentifier(list.get(i).getImg(),"mipmap",context.getPackageName()));
        viewHolder.textView.setText(list.get(i).getTitle());
        viewHolder.collect.setImageResource(R.mipmap.tab31);
        return view;
    }

    class ViewHolder{
        ImageView img;
        TextView textView;
        ImageView collect;
    }
}
